#!/bin/sh
set -x

# Clone the source repository
rm -rf source_data
git clone https://github.com/disposable-email-domains/disposable-email-domains.git source_data && \
# Create what would be generated by source_data
python bin/parse_source_data > disposable_email_domains/__init__.py && \

# See if anything has changed
git diff --exit-code

CHANGES=$?

if [ -z "${TEST_ONLY}" ] && [ $CHANGES != 0 ]; then
    # Git setup
    git config --global user.email 'dee-eye@users.noreply.github.com'
    git config --global user.name "Dee Eye"
    git remote rm origin
    git remote add origin https://dee-eye:$OAUTH_TOKEN@github.com/disposable-email-domains/python-disposable-email-domains.git

    # Bump the version
    pip install bump
    VERSION=`bump`

    # Add changes, push and make a PR
    git checkout -b version-$VERSION
    git commit -am "Version $VERSION"
    git push -f origin HEAD
    curl --show-error --fail -u "dee-eye:$OAUTH_TOKEN" --data '{"title": "'"Version $VERSION"'", "head": "'"version-$VERSION"'", "base": "master"}' https://api.github.com/repos/disposable-email-domains/python-disposable-email-domains/pulls
    exit $?
else
    exit $CHANGES
fi
